﻿using TrueSync;
using System.Collections.Generic;

public enum StateEnum
{
    Init,
    Stand,
    Crouch,
    Jump,
    JumpForward,
    JumpBackward,
    WalkForward,
    WalkBackward,
    DefenseState,
    StandDefense,
    CrouchDefense,
    DefenseStateEnd,
    HitState,
    StandHit,
    StandDefenseHit,
    CrouchHit,
    CrouchDefenseHit,
    JumpHit,
    KnockdownHit,
    ThrowHit,
    FloatHit,
    HitFly,
    CounterAttackHit,
    HitStateEnd,
    Dizzy,
    Dead,

    SkillStream,
}


public abstract class StateBase
{
    public List<string> AnimationNames;

    protected LogicEntity mLEntity;
    protected LogicStatusComponent statusComponent;
    private LogicAnimationComponent mAnimationComponent;
    private int mAnimationIndex;

    public abstract StateEnum StateName
    {
        get;
    }

    public virtual void Enter(LogicEntity lEntity, StateBase lastState, object[] stateParams)
    {
        mLEntity = lEntity;
        statusComponent = lEntity.GetLogicComponent<LogicStatusComponent>();
        mAnimationComponent = lEntity.GetLogicComponent<LogicAnimationComponent>();
        if (AnimationNames.Count > 0)
        {
            mAnimationComponent.ChangeToAnimation(AnimationNames[0]);
        }
        mAnimationIndex = 0;
    }

    public virtual void Excute(FP deltaTime)
    {
    }

    public virtual void Exit()
    {
        mLEntity = null;
        statusComponent = null;
    }

    public virtual void OnInputDirection(int direction)
    {
    }

    public abstract void OnStimulate(Stimulate stimulate);

    public virtual void OnCurrentAnimationFinished()
    {
        mAnimationIndex++;
        if(mAnimationIndex < AnimationNames.Count)
        {
            mAnimationComponent.ChangeToAnimation(AnimationNames[0]);
        }
        else
        {
            mAnimationComponent.ChangeToAnimation(null);
        }
    }

    public virtual bool IsFinished()
    {
        return mAnimationIndex >= AnimationNames.Count;
    }

    public virtual void Serialize(EByte ms)
    {
        ms.WriteInt(AnimationNames.Count);
        for(int i = 0; i < AnimationNames.Count; i++)
        {
            ms.WriteString(AnimationNames[i]);
        }
    }

    public virtual void Deserialize(EByte ms)
    {
        int animationCounts = ms.ReadInt();
        AnimationNames = new List<string>(animationCounts);
        for(int i = 0; i < animationCounts; i++)
        {
            AnimationNames.Add(ms.ReadString());
        }
    }
}
